#ifndef __PROTOCOL_485_COMMAND_DEAL_H__
#define __PROTOCOL_485_COMMAND_DEAL_H__
#include <typescommon.h>
#include <ptcPackParse.h>

#define masterCmdStructName(cmdname) masterStruct##cmdname
#define devCmdStructName(cmdname) devStruct##cmdname
#define masterCmdCvrtFuncName(cmdname) masterCmdCvrt##cmdname
#define devCmdCvrtFuncName(cmdname) devCmdCvrt##cmdname
#define devCmdDealFuncName(cmdname) devCmdDeal##cmdname
#define ptcCmdFuncEnumDef(cmdname) ptcCmdFunc_##cmdname

enum ptcCmdFuncEnum
{
    ptcCmdFuncEnumDef(SetReadSN) = 0,
    ptcCmdFuncEnumDef(ReadBaseInfo),
    ptcCmdFuncEnumDef(Heartbeat),
    ptcCmdFuncEnumDef(WriteFileBegin),
    ptcCmdFuncEnumDef(WriteFileData),
    ptcCmdFuncEnumDef(WriteFileEnd),
    ptcCmdFuncEnumDef(ReadFileBegin),
    ptcCmdFuncEnumDef(ReadFileData),
    ptcCmdFuncEnumDef(ReadFileEnd),
};

enum commandExcuteResultEnum
{
    cmdExeRslt_OK = 0,
    cmdExeRslt_Fail,
    cmdExeRslt_MasterSizeErr,
    cmdExeRslt_DevSizeErr,
    cmdExeRslt_FileSizeErr,
    cmdExeRslt_Unkown = 0xffff,
};

// struct ptcCmdHeadStruct
// {
//     uint16_t commandNB;// number
//     uint8_t commandSN; // 16byte, not change.for diffrent
//     uint8_t responseFlag;
//     uint8_t commandData[1];
// };
#define PTC_CMD_HEAD_PST_CMD_NUMB 0
#define PTC_CMD_HEAD_PST_CMD_SN 2
#define PTC_CMD_HEAD_PST_RESP 3
#define PTC_CMD_HEAD_PST_DATA 4

struct ptcCmdOptStruct
{
    const uint8_t *deviceSerialNumber;
    struct ptcPackOption parser;
};

#define ptcCmdInstanceDef(cname, devSN, parseSize) \
    uint8_t pname##ParsSaveBuffer[parseSize];                \
    struct ptcCmdOptStruct cname = {devSN, {pname##ParsSaveBuffer, &cname, DEVICE_MCU_COMMON_STR_SIZE, 0, 0, protocolParseCommonStep_FixedHead, protocolParseCommonVersion1}};

ptcPackType ptcCmdGetRemainSize(struct ptcCmdOptStruct *cmdOpt);

int ptcCmdCheckAddress(struct ptcCmdOptStruct *cmdOpt);

ptcPackType ptcCmdGetUserSize(struct ptcCmdOptStruct *cmdOpt);
int ptcCmdSetUsrSize(struct ptcCmdOptStruct *cmdOpt, ptcPackType setsize);

void *ptcCmdGetUsrPtr(struct ptcCmdOptStruct *cmdOpt);
int ptcCmdSetUsrData(struct ptcCmdOptStruct *cmdOpt, const void *usrdata, ptcPackType usrsize);

uint16_t ptcCmdGetCmdNB(struct ptcCmdOptStruct *cmdOpt);
int ptcCmdSetCmdNB(struct ptcCmdOptStruct *cmdOpt, uint16_t cmdid);

uint8_t ptcCmdGetCmdSn(struct ptcCmdOptStruct *cmdOpt);
int ptcCmdSetCmdSn(struct ptcCmdOptStruct *cmdOpt, uint8_t cmdserial);

uint8_t ptcCmdGetCmdResp(struct ptcCmdOptStruct *cmdOpt);
int ptcCmdSetCmdResp(struct ptcCmdOptStruct *cmdOpt, uint8_t cmdResp);

#endif
